﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.TextMining.EnumerationObjects
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class ExtractionOutput:ResultBase
    {
        #region props
        private int _FileProcessed;
        /// <summary></summary>
        public int FileProcessed
        {
            get
            {
                return this._FileProcessed;
            }
            set
            {
                this._FileProcessed = value;
            }
        }

        private int _PersonExtracted;
        /// <summary></summary>
        public int PersonExtracted
        {
            get
            {
                return this._PersonExtracted;
            }
            set
            {
                this._PersonExtracted = value;
            }
        }

        private int _FileScanned;
        /// <summary></summary>
        public int FileScanned
        {
            get
            {
                return this._FileScanned;
            }
            set
            {
                this._FileScanned = value;
            }
        }

        private string _LogFilePath;
        /// <summary></summary>
        public string LogFilePath
        {
            get
            {
                return this._LogFilePath;
            }
            set
            {
                this._LogFilePath = value;
            }
        }

        private string _ErrorFilePath;
        /// <summary></summary>
        public string ErrorFilePath
        {
            get
            {
                return this._ErrorFilePath;
            }
            set
            {
                this._ErrorFilePath = value;
            }
        }
        #endregion

        #region ctor
        /// <summary>
        /// 
        /// </summary>
        public ExtractionOutput():base(Guid.Empty,Guid.Empty)
        {
            this._FileProcessed = 0;
            this._FileScanned = 0;
            this._PersonExtracted = 0;
            this._LogFilePath = string.Empty;
            this._ErrorFilePath = string.Empty;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="componentID"></param>
        /// <param name="jobTicketID"></param>
        /// <param name="fileScanned"></param>
        /// <param name="fileProcessed"></param>
        /// <param name="personExtracted"></param>
        /// <param name="logFilePath"></param>
        /// <param name="errFilePath"></param>
        public ExtractionOutput(Guid componentID, Guid jobTicketID, 
            int fileScanned,
            int fileProcessed,
            int personExtracted,
            string logFilePath,
            string errFilePath) 
            : base(componentID, jobTicketID)
        {
            this._FileScanned = fileScanned;
            this._FileProcessed = fileProcessed;
            this._PersonExtracted = personExtracted;
            this._LogFilePath = logFilePath;
            this._ErrorFilePath = errFilePath;
        }
        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xDoc"></param>
        /// <param name="parentNode"></param>
        /// <returns></returns>
        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Output");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FinishTime", this.FinishTime.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "LogFilePath", this.LogFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ErrorFilePath", this.ErrorFilePath);

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FileScanned", this.FileScanned.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FileProcessed", this.FileProcessed.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "PersonExtracted", this.PersonExtracted.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "LogFilePath", this.LogFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ErrorFilePath", this.ErrorFilePath);

            return xNode;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xNode"></param>
        /// <returns></returns>
        public override ResultBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                ExtractionOutput output = new ExtractionOutput();
                // output.OwnerComponentID = new Guid(xNode.Attributes.GetNamedItem("ComponentID").Value);
                output.FileScanned = int.Parse(XmlDataUtil.GetAttributeValue(xNode, "FileScanned", "0"));
                output.FileProcessed = int.Parse(XmlDataUtil.GetAttributeValue(xNode, "FileProcessed", "0"));
                output.PersonExtracted = int.Parse(XmlDataUtil.GetAttributeValue(xNode, "PersonExtracted", "0"));
                output.LogFilePath = XmlDataUtil.GetAttributeValue(xNode, "LogFilePath", "");
                output.ErrorFilePath = XmlDataUtil.GetAttributeValue(xNode, "ErrorFilePath", "");
                return output;
            }
            else
            {
                throw new Exception("Unable to instantiate output: invalid type specified in xml node");
            }
        }
    }
}
